package io.dbw.modules.job.controller;

import io.dbw.common.annotation.SysLog;
import io.dbw.common.enums.BusinessTypeEnum;
import io.dbw.common.utils.PageUtils;
import io.dbw.common.utils.Result;
import io.dbw.modules.business.controller.sys.AbstractController;
import io.dbw.modules.job.entity.ScheduleJobEntity;
import io.dbw.modules.job.service.ScheduleJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务
 *
 * @author
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController extends AbstractController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 分页查询定时任务
     *
     * @param params 查询参数
     * @return Result
     */
    @GetMapping("/list")
    //@RequiresPermissions("sys:schedule:list")
    public Result<ScheduleJobEntity> list(@RequestParam Map<String, Object> params) {
        PageUtils<ScheduleJobEntity> page = scheduleJobService.queryPage(params);

        return writeSuccess(page) ;
    }

    /**
     * 根据主键查询详情
     *
     * @param jobId 主键
     * @return Result
     */
    @GetMapping("/info/{jobId}")
    //@RequiresPermissions("sys:schedule:info")
    public Result info(@PathVariable("jobId") String jobId) {
        ScheduleJobEntity schedule = scheduleJobService.getById(jobId);

        return writeSuccess(schedule );
    }

    /**
     * 新增定时任务
     *
     * @param scheduleJob scheduleJob
     * @return Result
     */
    @SysLog(value = "定时任务",businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/save")
    //@RequiresPermissions("sys:schedule:save")
    public Result save(@RequestBody ScheduleJobEntity scheduleJob) {
        scheduleJobService.add(scheduleJob);

        return Result.success();
    }

    /**
     * 修改定时任务
     *
     * @param scheduleJob scheduleJob
     * @return Result
     */
    @SysLog(value = "定时任务",businessType = BusinessTypeEnum.UPDATE)
    @PostMapping("/update")
    //@RequiresPermissions("sys:schedule:update")
    public Result update(@RequestBody ScheduleJobEntity scheduleJob) {

        scheduleJobService.update(scheduleJob);

        return Result.success();
    }

    /**
     * 删除定时任务
     *
     * @param jobIds jobIds
     * @return Result
     */
    @SysLog(value = "定时任务",businessType = BusinessTypeEnum.DELETE)
    @PostMapping("/delete")
    //@RequiresPermissions("sys:schedule:delete")
    public Result delete(@RequestBody String[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);

        return Result.success();
    }

    /**
     * 立即执行任务
     *
     * @param jobIds jobIds
     * @return Result
     */
    @SysLog("立即执行任务")
    @PostMapping("/run")
    //@RequiresPermissions("sys:schedule:run")
    public Result run(@RequestBody String[] jobIds) {
        scheduleJobService.run(jobIds);

        return Result.success();
    }

    /**
     * 暂停定时任务
     *
     * @param jobIds jobIds
     * @return Result
     */
    @SysLog("暂停定时任务")
    @PostMapping("/pause")
    //@RequiresPermissions("sys:schedule:pause")
    public Result pause(@RequestBody String[] jobIds) {
        scheduleJobService.pause(jobIds);

        return Result.success();
    }

    /**
     * 恢复定时任务
     *
     * @param jobIds jobIds
     * @return Result
     */
    @SysLog("恢复定时任务")
    @PostMapping("/resume")
    //@RequiresPermissions("sys:schedule:resume")
    public Result resume(@RequestBody String[] jobIds) {
        scheduleJobService.resume(jobIds);

        return Result.success();
    }
}
